<%@ page language="java" contentType="text/html; charset=utf-8"	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags/sys" prefix="sys"%>
<%@ taglib uri="/WEB-INF/tlds/webui.tld" prefix="webui"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}"	scope="request" />

<jsp:include page="editRole.jsp" />

<webui:panel id="roleSearch">
	<webui:form method="get" styleClass="form-horizontal" 
		name="roleSearchForm" id="roleSearchForm"
		action="${contextPath}/rest/roles/search/search?sort=id,desc">
		<webui:input label="名称" styleClass="col-sm-6" labelClass="col-sm-3"
			inputClass="col-sm-9" type="text" name="name">
		</webui:input>
		<webui:button label="" type="button" styleClass="col-sm-4" 
			labelClass="col-sm-8" inputClass="col-sm-4" buttonClass="btn-primary"
			onclick="createRole()">添加角色
		</webui:button>
		<webui:button label="" type="button" styleClass="col-sm-2" 
			labelClass="col-sm-7" inputClass="col-sm-5" buttonClass="btn-primary"
			onclick="tb_roles.filter()">查询
		</webui:button>
	</webui:form>
	
	<webui:dialog id="permissDialog" title="权限修改">
		<div id="orgTree"></div>
		<webui:toolbar footer="true">
			<webui:button type="button" buttonClass="btn-white" onclick="cancelPermission()">取消</webui:button>
			<webui:button id="permissionSaveBtn" type="button" buttonClass="btn-primary" ladda="true" onclick="updatePermission()">更新</webui:button>
		</webui:toolbar>
	</webui:dialog>
	
	<webui:table id="tb_roles" datas="_embedded.roles" editable="true"
		dataUrl="${contextPath}/rest/roles/search/search?sort=id,desc"
		postUrl="${contextPath}/sys/role/save.action"
		searchForm="#roleSearch" editorForm="#roleEditor">
		<webui:column field="id" title="" styleClass="display-none">
			<input type="checkbox" v-bind:value="row.id" />
		</webui:column>
		<webui:column editor="tableCheck" format="row.id"></webui:column>
		<webui:column field="id" editor="hidden" styleClass="display-none">{{row.id}}</webui:column>
		<webui:column field="name" editor="text" title="名称" style="width:25%">{{row.name}}<webui:validator identity="Role:name" required="true" minlength="2" maxlength="10">{identity:'角色名称已存在'}</webui:validator></webui:column>
		<webui:column field="desc" editor="text" title="描述" style="width:25%">{{row.desc}}<webui:validator required="true" minlength="2" maxlength="40"/></webui:column>
		<webui:column field="status" title="状态" style="width:25%">{{row.status==0?'正常':'删除'}}</webui:column>
		<webui:column title="操作" editor="opt" style="width:25%">
			<button v-if="row.status==0" class="btn btn-xs btn-warning row-append-edit-btn-hide" v-bind:onclick="'javascript:editPermission('+row.id+')'" type="button">权限管理</button>
			<button v-if="row.status==0" class="btn btn-xs btn-danger row-append-edit-btn-hide" v-bind:onclick="'javascript:deleteRole('+row.id+',\''+row.name+'\')'" type="button">删除</button>
		</webui:column>
	</webui:table>
</webui:panel>

<script>
	function deleteRole(id, title){
		confirm('确认删除'+title+'?', function(result){
	        	$.ajax({
		 	 	    type:"POST",
		 	 	    url:"${contextPath}/sys/role/status.action",
					dataType:"json",
					contentType:"application/json; charset=utf-8",
					data:'{"id":"'+id+'","status":"-1"}',
		 	 	    success:function(data){
		 	 	    	tb_roles.reload();
		 	 	    	showMessage('提示', '删除'+title+'成功！');  			 	 	    		
		 	 	    }
		 	 	}); 			                
	    });
	 }

	var roleId;
 	function editPermission(id){
 		roleId = id;
 		$("#orgTree").jstree("destroy");
 		$('#orgTree').jstree({
			'core' : {
				'data' : {
						"url" : '${contextPath}/sys/permission/tree/' + id + '.action',
						"data" : function (node) {
							return { "id" : node.id };
						}
					}
			}
			,"plugins" : ['checkbox', 'sort', 'types', 'wholerow', 'search', 'unique'],	  	
		});
 		$('#orgTree').jstree(true).uncheck_all();
    	$('#permissDialog').modal('show');
 	}

 	function cancelPermission(){
 		permissionSaveBtn.ladda('stop');
 		$('#permissDialog').modal('hide');
 	}

 	function updatePermission(){
 		permissionSaveBtn.ladda('start');
 		var menus = $('#orgTree').jstree(true).get_checked(false);
 		var obj = new Object();
 		var resIds = new Array();
 		obj.objId = roleId;
 		obj.objType = "role";
 		obj.resType = "menu";
		for(var i=0;i<menus.length;i++){
			resIds.push(menus[i]);
		}
		obj.resIds = resIds;
		$.ajax({
 	 	    type:"POST",
 	 	    async:false,
			dataType:"json",
			contentType:"application/json; charset=utf-8",
 	 	    url:"${contextPath}/sys/permission/save.action",
			data:JSON.stringify(obj),
 	 	    success:function(data){
 	 	 		permissionSaveBtn.ladda('stop');
 	 	    	$('#permissDialog').modal('hide');
 	 			showMessage('提示','权限更新成功。',	function(){
 	 				tb_roles.refresh();
 	 			});
 	 	    }
 	 	});
 	}
</script>